package com.yry.timertask.chapter34;

import java.util.Timer;
import java.util.TimerTask;

public class WaterRobot extends TimerTask {
	private Timer timer;

	public WaterRobot(Timer timer) {
		this.timer = timer;
	}

	//最大容量为5L
	private  Integer bucketCapacity = 0;
	@Override
	public void run() {
		//灌水直至桶满为止
		if(bucketCapacity<5){
			System.out.println("Add 1L water into the bucket");
			bucketCapacity++;
		}else{
			System.out.println("The number of canceled timertask in timer is："+timer.purge());
			cancel();//水满后就停止
			System.out.println("The waterRobot has been aborted");
			System.out.println("The number of canceled timertask in timer is："+timer.purge());
			System.out.println("Current water is "+ bucketCapacity+"L");
			//等待2S,终止timer里面的所有内容
			try {
				Thread.sleep(2000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			timer.cancel();
		}
	}
}
